<template>
	<view class="index" :style="{'--color':vuex_config.themeColor1}">
		<template v-if="init && dataForm.pk_id">
			<view class="header">
				<image class="banner" src="../static/ic_bg.png" mode=""></image>
				<view class="header-box">
					<image class="header-img" :src="vuex_user.headimgurl" mode=""></image>
					<view class="header-name">{{dataForm.name}}</view>
				</view>
			</view>
			<view class="pay-box">
				<view class="title">消费金额</view>
				<view class="input-box">
					<text class="input-box-icon">￥</text>

					<view class="input_view">{{price}}</view>
				</view>
			</view>
			<view class="conpon-box">
				<view class="conpon-box-left">
					<image class="left-icon" src="../static/ic_1.png" mode="widthFix"></image>
					<view class="">优惠金额</view>
				</view>
				<view class="conpon-box-right">{{Number(discount_amount).toFixed(2)}}元</view>
			</view>

			<view>

				<key-board v-model='price' @paymentClick="onPay"></key-board>
			</view>
		</template>


		<sww-login></sww-login>

	</view>
</template>

<script>
	import {
		payOrder
	} from '@/mixins/payOrder';

	import {
		amount
	} from '@/utils/validate.js'
	import keyBoard from './components/numberKeyboard.vue'
	export default {
		mixins: [payOrder],
		data() {
			return {
				style: {
					width: '500rpx'
				},
				price: '',
				wx_obj: {},
				dataForm: {},
				init: false
			}
		},
		components: {
			'key-board': keyBoard
		},
		async onLoad(option) {
			console.log(option)
			if (option.q) {
				let a = decodeURIComponent(option.q).split('qrcode/')
				this.$resPost('GetCollectionQrcodeoInfo', {
					code: a[1]
				}).then(res => {
					if (!res.data.pk_id) return this.$toast('该店铺二维码未启用')
					this.dataForm = res.data

					// #ifdef MP-WEIXIN
					this.$resPost('GetBusinessInfo', {
						business_id: undefined,
						appid: JSON.parse(this.dataForm.wechat_json).appid
					}).then(({
						data
					}) => {
						this.wx_obj = data
					})
					// #endif
				})
			}
			uni.setNavigationBarTitle({
				title: this.vuex_business.name
			});
			this.checkLogin()
		},
		computed: {
			discount_amount() {
				return (this.dataForm.discount / 100) * (this.price || 0)
			},
		},
		methods: {
			checkLogin() {
				this.$login('checkLogin', {}, 'uniBack').then(res => {
					this.init = true
				})
			},

			onPay() {

				this.onSubmit()
			},




			onSubmit(e) {
				let {
					price,
					discount_amount
				} = this

				price = (Number(price) - Number(discount_amount)).toFixed(2)


				// #ifdef MP-ALIPAY
				let pay_type = 7;
				let appid = JSON.parse(this.dataForm.alipay_json).appid
				let params = {
					remark: `线下收款${price}元`,
					type: this.vuex_business.mchid,
					price: price, // 订单总金额
					discount_amount: Number(discount_amount).toFixed(2),
					store_id: this.dataForm.pk_id,
					appid,
				};

				// #endif

				// #ifdef MP-WEIXIN
				let pay_type = 1
				let appid = JSON.parse(this.dataForm.wechat_json).appid
				let params = {
					remark: `线下收款${price}元`,
					type: this.dataForm.wechat_mchid,
					price: price, // 订单总金额
					discount_amount: Number(discount_amount).toFixed(2),
					store_id: this.dataForm.pk_id,
					appid,
				};

				// #endif
				if (!this.wx_obj.mchid) return this.$toast('未获取到支付信息')
				if (Number(discount_amount) > 0) params.remark = params.remark + ',' + '优惠了' + Number(discount_amount)
					.toFixed(2) + '元'
				this._submitOrder(params, {
					totalPrice: price,
					pay_type,
					orderApi: 'AddCollectionOrder',
					updateApi: 'UpdateCollectionOrderStatus',
					order_type: 1,
					router: 'switchTap',
					redirectUrl: '/pages/index/index',
					modal: false,
					payErr: false,

					// #ifdef MP-WEIXIN
					creatPayApi: 'GetServiceProviderCreateOrderPay',
					parameter_obj: {
						sub_appid: appid,
						mch_id: this.wx_obj.mchid,
						sign_key: this.wx_obj.payment_key,
						sub_mch_id: this.dataForm.wechat_mchid,
						business_id: undefined,
						users_id: undefined
					},
					// #endif
					updateParams: {
						pay_type,
						// #ifdef MP-ALIPAY
						pay_type: 5,
						// #endif
						pay_status: 1,
						type: undefined,
					},
				})

			},
			getResult(e) {
				this.price = e
			},
		}
	}
</script>
<style>
	page {
		background-color: #F7F7F7;
	}
</style>
<style lang="scss" scoped>
	.index {
		padding: 50rpx 30rpx 0;

		.header {
			padding-bottom: 34rpx;
			width: 100%;
			height: 250rpx;
			position: relative;

			.banner {
				width: 100%;
				height: 100%;
			}

			.header-box {
				position: absolute;
				top: 66rpx;
				left: 40rpx;
				display: flex;
				align-items: center;

				.header-img {
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
					border: 2px solid #E7C69D;
				}

				.header-name {
					font-size: 30rpx;
					font-weight: bold;
					color: #F4D1A6;
					padding-left: 24rpx;
				}
			}
		}

		.pay-box {
			width: 100%;
			padding: 34rpx 30rpx 34rpx;
			background-color: #fff;
			border-radius: 12rpx;

			.title {
				width: 100%;
				font-size: 30rpx;
				font-weight: bold;
				color: #000000;
				padding-bottom: 34rpx;
				border-bottom: 1px dashed #E5E5E5;
			}

			.input-box {
				width: 100%;
				// display: flex;
				// align-items: flex-end;
				padding-top: 34rpx;

				.input-box-icon {
					display: inline-block;
					font-size: 50rpx;
					padding-right: 20rpx;
				}

				@keyframes keyName {

					0% {
						opacity: 1;
					}

					50% {
						opacity: 0.4;
					}

					100% {
						opacity: .1;
					}

				}

				.input_view {
					display: inline-block;
					font-size: 40rpx;
					font-weight: bold;
					position: relative;

					&::after {
						animation: keyName 1s linear infinite;
						content: '';
						position: absolute;
						right: -8rpx;
						top: 50%;
						transform: translateY(-50%);
						background-color: #000;
						height: 70%;
						width: 2rpx;
					}
				}
			}
		}

		.conpon-box {
			width: 100%;
			display: flex;
			align-items: center;
			justify-content: space-between;
			margin-top: 42rpx;

			.conpon-box-left {
				display: flex;
				align-items: center;
				font-size: 24rpx;
				font-weight: bold;
				color: #000000;

				.left-icon {
					width: 34rpx;
					margin-right: 10rpx;
					position: relative;
					top: 4rpx;
				}
			}

			.conpon-box-right {
				font-size: 24rpx;
				font-weight: bold;
				color: red;
			}
		}

		.hide {
			visibility: hidden;
		}
	}
</style>
